package org.walkerljl.boss.service.identity.impl;

import org.springframework.stereotype.Service;
import org.walkerljl.boss.support.common.log.SysLog;
import org.walkerljl.boss.dao.identity.AppDao;
import org.walkerljl.boss.domain.identity.App;
import org.walkerljl.boss.support.dao.BaseDao;
import org.walkerljl.boss.support.domain.Page;
import org.walkerljl.boss.support.service.impl.JqueryDatatableBaseServiceImpl;
import org.walkerljl.boss.service.identity.AppService;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * AppServiceImpl
 *
 * @author lijunlin
 */
@Service("appService")
public class AppServiceImpl extends JqueryDatatableBaseServiceImpl<Long, App> implements AppService {

    @Resource
    private AppDao appDao;

    @Override
    public BaseDao<Long, App> getDao() {
        return appDao;
    }

    @Override
    public Long insert(App app) {
        app.setToken(generateAppToken());
        return super.insert(app);
    }

    @Override
    public Long insert(App app, SysLog sysLog) {
        app.setToken(generateAppToken());
        return super.insert(app);
    }

    /**
     * Generate app token
     * @return
     */
    private String generateAppToken() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    @Override
    public List<App> selectAccessibleApps(String userId) {
        App condition = new App();
        condition.setManagerId(userId);
        return selectList(condition);
    }

    @Override
    public List<App> select100Apps() {
        App condition = new App();
        condition.setCurrentPage(0);
        condition.setPageSize(100);
        condition.setQueryTotalCount(false);
        Page<App> page = selectPage(condition);
        return page == null ? null : page.getResultList();
    }
}